var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp()

Page({
  data:{
    refundBox:false,
    orderGoodsId:0,
    returnCont:{},
    returnNum:0,
    logisticsBox:false,
    returnPrice:0.00,
    returnReasonList:[],
    uploadImgCount:0,
    logisticsList:'',
    returnSelectReason:'请选择退货原因',
    returnValue:'',
    returnRemark:'',
    logName:'请选择物流',
    logId:'',
    logisticsNumber:'',
    productInfo:[]
  },
  refundClose:function(){
    this.setData({
      refundBox:false,
      logisticsBox:false
    })
  },
  logSelect: function (event){
    this.setData({
      logName: event.target.dataset.name,
      logId: event.target.dataset.id
    });
    this.refundClose();
  },
  refundSubmit:function(){
    let that = this;
    util.request(api.ReturnSubmit, { id: that.data.orderGoodsId, reasonCode: that.data.returnValue, type: 2, number: that.data.returnNum, remark: that.data.returnRemark, picUrls: that.data.productInfo, shippingId: that.data.logId, shippingNo:that.data.logisticsNumber},"POST",'application/json').then(function (res){
      if (res.errno === 0){
        wx.navigateTo({
          url: '/pages/ucenter/order/order'
        });
      }else{
        util.showErrorToast(res.errmsg);
      }
    })
  },
  sureLogistics:function(){
    let that = this;
    this.setData({
      logisticsBox: true
    })
    util.request(api.SelectLogistics).then(function (res){
      if(res.errno === 0){
        that.setData({
          logisticsList:res.data
        })
      }else{
        util.showErrorToast(res.msg);
      }
    })
  },
  refundExplain:function(e){
    this.setData({
      returnRemark:e.detail.value
    })
  },
  logisticsNumberCode: function (e){
    this.setData({
      logisticsNumber: e.detail.value
    })
  },
  onLoad: function (options) {
    this.setData({
      orderGoodsId:options.id
    });
    this.getReturnApply();
  },
  doUpload: function () {
    let that=this;
    wx.chooseImage({
      count:1,
      sizeType: ['compressed'], 
      sourceType: ['album', 'camera'],  
      success: function (res) {
        let tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        for (let i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url:api.UploadPhoto,
            filePath:tempFilePaths[i],
            name:'file',
            formData:{'imgIndex':i},
            header:{"Content-Type":"multipart/form-data",'X-WX-SHOP-Token':wx.getStorageSync('token')},
            success: function(res){
              wx.hideToast();
              that.setData({
                uploadImgCount: that.data.uploadImgCount + 1
              });
              let data = JSON.parse(res.data);
              let productInfo = that.data.productInfo;
              if (data.code==0){
                productInfo.push({ "url": data.url });
                that.setData({
                  productInfo: productInfo
                }); 
              }else{
                util.showErrorToast(res.msg);
              }
            },
            fail:function (res){
              wx.showModal({
                title:'错误提示',
                content:'上传图片失败',
                showCancel:false,
                success:function (res){}
              })
            }
          });
        }
      }
    })
  },
  upLoaderDel: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let upImg = that.data.productInfo;
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          upImg.splice(index, 1);
          that.setData({
            uploadImgCount: that.data.uploadImgCount - 1
          });
        } else if (res.cancel) {
          return false
        }
        that.setData({
          productInfo:upImg
        });
      }
    })
  },
  refundSelect:function(event){
    this.setData({
      returnSelectReason: event.target.dataset.name,
      returnValue: event.target.dataset.reason
    });
    this.refundClose();
  },
  selectReason:function(){
    let that = this;
    util.request(api.ReturnReason).then(function (res){
      if (res.errno === 0){
        that.setData({
          returnReasonList:res.data
        });
      }else{
        util.showErrorToast(res.errmsg);
      }
    });
    this.setData({
      refundBox:true
    })
  },
  cutNumber: function (event) {
    let returnIndex = event.target.dataset.index;
    let number = (returnIndex - 1 > 0) ? returnIndex - 1 : 0;
    this.setData({
      returnNum: number
    });
    if (number == 0) {
      this.setData({
        returnPrice: 0.00
      });
    }
    this.getReturnMoney();
  },
  addNumber: function (event) {
    let returnIndex = event.target.dataset.index;
    let number = returnIndex + 1;
    this.setData({
      returnNum: number
    });
    if (number > this.data.returnCont.number) {
      this.setData({
        returnNum: number - 1
      });
      util.showErrorToast('数量超出操出可选范围');
    } else {
      this.getReturnMoney();
    }
  },
  getReturnApply(){
    let that = this;
    util.request(api.ReturnApply, {
      orderGoodsId:that.data.orderGoodsId
    }).then(function (res){
      if (res.errno === 0){
        that.setData({
          returnCont:res.data
        });
      }else{
        util.showErrorToast(res.errmsg);
      }
    });
  },
  getReturnMoney:function(){
    let that = this;
    util.request(api.ReturnMoney, {
      orderGoodsId: that.data.orderGoodsId,
      number:that.data.returnNum
    }).then(function (res){
      if (res.errno === 0){
        that.setData({
          returnPrice: res.data.toFixed(2)
        });
      } else {
        util.showErrorToast(res.errmsg);
      }
    });
  },
  onReady: function(){

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  }
})